package database

import (
	"fmt"
	"gin/model"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var DB *gorm.DB

func Mysql_config() {
	var err error
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
		//"root",
		//"mysql_PFpn8J",
		//"47.104.190.71",
		//"3306",
		//"business",
		"root",
		"123456",
		"127.0.0.1",
		"3306",
		"business",
	)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Printf("数据库连接失败\n")
		return
	}
	fmt.Printf("数据库连接成功\n")
}

// 111
func AutoMigrate() {
	err := DB.AutoMigrate(
		new(model.User),
		new(model.Roles),
		new(model.Permission),
		new(model.UserRole),
		new(model.RolePermission),
		new(model.Log),
	)
	if err != nil {
		fmt.Println("数据迁移失败")
		return
	}
	fmt.Println("数据迁移成功")

}
